package cn.com.czpb.client.custorm.webkit;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import cn.com.czpb.client.R;


/**
 * Created by zhousz on 2017/7/14.
 */

public abstract class RbWebViewClient extends WebViewClient {

    private Activity act;

    public RbWebViewClient(Activity activity) {
        act = activity;
    }

    @Override
    public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error) {
        //证书认证失败，提示用户是否继续
        AlertDialog.Builder builder = new AlertDialog.Builder(act);
        AlertDialog alertDialog = builder.create();
//        String message = act.getString(R.string.rb_ssl_certificate_error_msg);
//        switch (error.getPrimaryError()) {
//            case SslError.SSL_UNTRUSTED:
//                message = act.getString(R.string.rb_ssl_certificate_not_trust);
//                break;
//            case SslError.SSL_EXPIRED:
//                message = act.getString(R.string.rb_ssl_certificate_has_expired);
//                break;
//            case SslError.SSL_IDMISMATCH:
//                message = act.getString(R.string.rb_ssl_certificate_host_not_match);
//                break;
//            case SslError.SSL_NOTYETVALID:
//                message = act.getString(R.string.rb_ssl_certificate_not_yet_valid);
//                break;
//        }
//
//        message += act.getString(R.string.rb_ssl_certificate_to_go_on);
//        alertDialog.setTitle(act.getString(R.string.rb_ssl_certificate_error));
//        alertDialog.setMessage(message);
        alertDialog.setCancelable(false);
//        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, act.getString(R.string.rb_go_on), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                // Ignore SSL certificate errors
//                handler.proceed();
//            }
//        });
//
//        alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, act.getString(R.string.rb_cancel), new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                handler.cancel();
//                act.finish();
//            }
//        });
//        alertDialog.show();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

        RbLineProgressBar progressBar = getProgressBar();
        if (progressBar != null)
            progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);

        RbLineProgressBar progressBar = getProgressBar();
        if (progressBar != null)
            progressBar.setVisibility(View.GONE);
    }

    protected abstract RbLineProgressBar getProgressBar();
}
